﻿
    using Chain.Wechat;
    using Newtonsoft.Json;
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
using Newtonsoft.Json.Linq;
    public partial class register : Page
    {


        public string GetAuthorize(string code)
        {
            string templateUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code";
            templateUrl = string.Format(templateUrl, PubFunction.curParameter.strWeiXinAppID, PubFunction.curParameter.strWeiXinAppSecret, code);
            HttpRequestHelper hrh = new HttpRequestHelper();
            return hrh.Reqeust(templateUrl);
        }

        public string GetUserInformation(string ACCESS_TOKEN, string OPENID, string zh_CN)
        {
            string templateUrl = "https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang={2}";
            templateUrl = string.Format(templateUrl, ACCESS_TOKEN, OPENID, zh_CN);
            HttpRequestHelper hrh = new HttpRequestHelper();
            return hrh.Reqeust(templateUrl);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string code = "";
            if (base.Request.QueryString["code"] != null)
            {
                code = base.Request.QueryString["code"].ToString();
            }
            if (code != "")
            {
                string getAuthorize = this.GetAuthorize(code);
                JObject obj = JObject.Parse(getAuthorize);
         
                this.OpenID.Value = "";
                if ((obj["openid"] != null) && (obj["openid"].ToString() != ""))
                {
                    this.OpenID.Value = obj["openid"].ToString();
                    this.Url.Value = string.Format("index.aspx?MemWeiXinCard={0}&random={1}", this.OpenID.Value, Guid.NewGuid());
                }
            }
            string state = "";
            if (base.Request.QueryString["state"] != null)
            {
                state = base.Request.QueryString["state"].ToString();
            }
            this.Recommended.Value = (state == "") ? "0" : state;
            if (Convert.ToInt32(this.Recommended.Value) != 0)
            {
                this.hidTJR.Value = "1";
                this.labRecommended.Visible = false;
                this.txtRecommended.Visible = false;
            }
        }
    }


